package com.itheima.demo07PreparedStatement实现增删改;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
目标:使用PreparedStatement实现增删改

讲解:
	添加数据: 向user表添加1条记录

	注意:只有字段值不确定才能给?
 */
public class Demo07 {
    public static void main(String[] args) throws SQLException {
        // 1.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_day2", "root", "root");
        // 2.编写参数化SQL(带?的SQL语句)
        String sql = "insert into user values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        // 3.给?赋值
        preparedStatement.setString(1,"wangwu");
        preparedStatement.setString(2,"1234");
        preparedStatement.setInt(3,3000);
        // 4.执行
        int rows = preparedStatement.executeUpdate();
        System.out.println(rows);
        // 5.释放资源
        preparedStatement.close();
        connection.close();
    }
}
